package com.elephant.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    //假装bruce的docket是这样的，组就叫bruce
    @Bean
    public Docket Docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("bruce");
    }

    @Bean
    public Docket Docket(Environment environment){
        //设置要显示的swagger环境
        Profiles profiles= Profiles.of("dev","test");
        //获取项目环境
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(ApiInfo())
                .enable(flag)
                .groupName("elephant")
                .select()
                //RequestHandlerSelectors配置扫描的方式
                //.basePackage("com.elephant.controller")扫描包
                //.withMethodAnnotation(GetMapping.class)扫描方法上有指定注解的
                //..withClassAnnotation(GetMapping.class)扫描类上有指定注解的
                .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
                .paths(PathSelectors.ant("/emp"))
                .build();
    }

    private ApiInfo ApiInfo(){
            //作者信息
        Contact contact = new Contact("elephant", "null", "Don't bother me.");

        return new ApiInfo("大象のApi文档",
                "不想过平凡人的生活就不要以平凡人的要求要求自己",
                "v1.0",
                "www.pornhub.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }


}
